/*
 * @Author: Wangjun
 * @Date: 2023-08-03 09:51:03SetExtend
 * @LastEditTime: 2023-10-13 11:20:23
 * @LastEditors: Wangjun
 * @Description:
 * @FilePath: \nodex\nodetime\op_ton.go
 * hnxr
 */
package nodetime

import (
	"gitee.com/hnxr_1/nodex"
)

func init() {
	nodex.Reister(new(TON))
}

type TON struct {
	nodex.Node
	X     nodex.Boolean `remark:"输入1"`
	Y     nodex.Boolean `remark:"输出Y,,out"`
	LastX bool
}

func (m *TON) GetModelID() int {
	return nodex.ModTimer
}

/**
 * @description:
 * @param {*nodex.Memory} mem 上一次缓存的结果
 * @return {*}
 */
func (m *TON) Init(mem *nodex.Memory) (err error) {
	m.UID = m.MakeUID()
	m.Ptr = &m.LastX

	if mem == nil {
		return
	}
	if mem.UID != m.UID {
		return
	}
	m.LastX = nodex.GetNumber[int64](mem.Ptr) != 0
	return
}

/**
 * @description:
 * @return {*}
 */
func (m *TON) GetName() string {
	return "TON"
}
func (m *TON) RegData() []byte {
	return nodex.MakeRegData(m, "上升沿脉冲", m.GetName(), "")
}

func (m *TON) Calc() (err error) {
	if m.EnQ {
		if *m.X.Bad {
			m.Bad = true
			return //不计算
		}
	}

	m.Bad = false

	if m.X.Value != m.LastX {
		m.LastX = m.X.Value
		if m.X.Value {
			m.Y.Value = true
			return
		}
	}
	m.Y.Value = false
	return
}
